package com.cskaoyan.demo3.service;

import com.cskaoyan.demo3.bean.MarketUser;
import com.cskaoyan.demo3.bean.MarketUserExample;
import com.cskaoyan.demo3.bean.PageInfo;
import com.cskaoyan.demo3.mapper.MarketUserMapper;
import com.cskaoyan.demo3.util.MybatisUtil;
import com.github.pagehelper.PageHelper;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

/**
 * @Description：
 * @Author：BLUEBLANK
 * @Date：2024/4/9 20:51
 */


public class AdminUerServiceImpl implements AdminUerService {
    @Override
    public List<MarketUser> query(MarketUser marketUser, PageInfo pageInfo) {
        SqlSession sqlSession = MybatisUtil.getSqlSession();
        MarketUserMapper mapper = sqlSession.getMapper(MarketUserMapper.class);
        // 获取marketUserMapper对象
        MarketUserExample example = new MarketUserExample();
        example.setDistinct(false);

        // 设置条件
        String username = marketUser.getUsername();
        String mobile = marketUser.getMobile();
        Integer userId = marketUser.getId();

        // 创建criteria对象
        MarketUserExample.Criteria criteria = example.createCriteria();

        if (username != null && !username.equals("")) {
            criteria.andUsernameLike("%" + username + "%");
        }

        if (mobile != null && !mobile.equals("")) {
            criteria.andMobileEqualTo(mobile);
        }

        if (userId != null) {
            criteria.andIdEqualTo(userId);
        }

        // 排序 order by add_time desc
        example.setOrderByClause(pageInfo.getSort() + " " + pageInfo.getOrder());

        // 分页 分不了页，就不分页了
        // 引入依赖 pagehelper
        PageHelper.startPage(pageInfo.getPage(), pageInfo.getLimit());



        List<MarketUser> marketUsers = mapper.selectByExample(example);
        sqlSession.close();
        return marketUsers;
    }
}
